package com.javacoo.xservice.base.support.handler.internal;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.stereotype.Component;

import com.javacoo.xservice.base.BaseResponse;
import com.javacoo.xservice.base.support.lock.Lock;
import com.javacoo.xservice.base.support.lock.config.LockConfig;
import com.javacoo.xservice.base.support.swaparea.SwapAreaUtils;
import com.javacoo.xservice.base.utils.LogUtil;

/**
 * 返回数据签名处理器
 * <p>说明:</p>
 * <li></li>
 * @author DuanYong
 * @since 2017年8月18日下午3:10:29
 */
@Component
@ConditionalOnBean(LockConfig.class)
public class MethodUnLockHandler extends AbstractHandler {
    /**锁对象*/
    @Autowired
    private Lock lock;
    /**方法加锁配置*/
    @Autowired
    private LockConfig lockConfig;

	@Override
	public void afterHandle(Object input) {
		if(!(input instanceof BaseResponse)){
			return;
		}
		BaseResponse rsp = (BaseResponse)input;
        String methodName = SwapAreaUtils.getSwapAreaData().getReqMethod();
        String cacheKey = SwapAreaUtils.getSwapAreaData().getCacheKey();
        if(lockConfig.getLockParamMap() == null || lockConfig.getLockParamMap().get(methodName) == null || StringUtils.isBlank(cacheKey)){
            return;
        }
        lock.unlock(cacheKey);
        LogUtil.info("方法解锁,MethodName:{},key:{}",methodName,cacheKey);
	}
    /**
     * 索引
     * <li>自然排序</li>
     *
     * @author duanyong@jccfc.com
     * @date 2021/3/4 11:06
     * @return: int
     */
    @Override
    public int index() {
        return 4;
    }
}
